package com.usian.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usian.article.mapper.ApArticleConfigMapper;
import com.usian.article.mapper.ApArticleContentMapper;
import com.usian.article.service.ArticleInfoService;
import com.usian.common.exception.CatchCustomException;
import com.usian.common.exception.code.UserStatusCode;
import com.usian.model.article.dtos.ArticleInfoDto;
import com.usian.model.article.pojos.ApArticleConfig;
import com.usian.model.article.pojos.ApArticleContent;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
@Service
@SuppressWarnings("ALL")
public class ArticleInfoServiceImpl implements ArticleInfoService {
    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;

    @Autowired
    private ApArticleContentMapper apArticleContentMapper;

    /**
     * 加载文章详情
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult loadArticleInfo(ArticleInfoDto dto) {
        Map<String, Object> resultMap = new HashMap<>();
        //1.检查参数
        if (dto == null || dto.getArticleId() == null) {
            //无效参数
            CatchCustomException.catchs(UserStatusCode.PARAM_FAIL, "无效参数");
        }

        //2.查询文章的配置
        ApArticleConfig apArticleConfig = apArticleConfigMapper.selectOne(Wrappers.<ApArticleConfig>lambdaQuery().eq(ApArticleConfig::getArticleId, dto.getArticleId()));//根据articleid查询文章配置对象
        if (apArticleConfig == null) {
            CatchCustomException.catchs(UserStatusCode.PARAM_FAIL, "文章配置信息不存在");
        }

        //3.查询文章的内容
        //判断文章是否被删除，或者是否被下架
        if (!apArticleConfig.getIsDelete() && !apArticleConfig.getIsDown()) {
            //如果没有根据articleid获取文章内容信息
            ApArticleContent apArticleContent = apArticleContentMapper.selectOne(Wrappers.<ApArticleContent>lambdaQuery().eq(ApArticleContent::getArticleId, dto.getArticleId()));
            //存入文章内容对象信息
            resultMap.put("content", apArticleContent);
        }
        //存入文章配置对象信息
        resultMap.put("config", apArticleConfig);
        //4.结果返回
        return ResponseResult.okResult(resultMap);
    }
}
